#include <GL/freeglut.h>

float angle = 0.0f;
void init()
{
    GLfloat light_ambient[] = {2.0, 0, 0}; // 环境光RGB
    GLfloat light_position[] = {1.0, 1.0, 1.0};

    glEnable(GL_DEPTH_TEST);
    glClearColor(0, 0, 0, 1);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
}

void reshape(int w, int h)
{
    // 防止窗口高度过小造成被零除错误
    if (0 == h)
        h = 1;
    GLfloat r = 1.0f * w / h;
    glViewport(0, 0, w, h); // 视口大小
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, r, 1.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1, 1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(-1.0f, 0, -10.0f);
    glRotatef(angle, 0, 1, 0);
    glutWireCube(1);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLoadIdentity();
    glTranslatef(1.0f, 0, -10.0f);
    glRotatef(angle, 0, 1, 0);
    glutSolidCube(1);
    glDisable(GL_LIGHTING);

    angle += 0.01f;
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(600, 400);
    glutCreateWindow("rotatecube");
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    init();
    glutMainLoop();
    return 0;
}
